# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import fields
from osv import osv
import wizard
import netsvc
import pooler
import time
from osv.orm import browse_record

class wiz_payment_from_client_open(wizard.interface): # Sua sau
    def _open_payment_from_client(self, cr, uid, data, context):
       
        cr.execute("Select kpfc.id from kdvn_payment_from_client kpfc\
                where contract_id in (%s)" %','.join(map(str,data['ids'])))
                                            
        payment_ids = []
        for p_id in cr.fetchall():
            payment_ids.append(p_id)
        payment_ids= ','.join(map(str,payment_ids))
        contract_ids  = ','.join(map(str,data['ids']))
        value = {
            'domain': "[('contract_id', 'in',["+contract_ids+"])]",
            'name': 'Payments from Client',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'kdvn.payment.from.client',
            'type': 'ir.actions.act_window',
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_payment_from_client, 'state':'end'}
        }
    }
wiz_payment_from_client_open('payment.from.client.open')


class wiz_contract_to_project_open(wizard.interface):
    def _open_project(self, cr, uid, data, context):
        pool_obj = pooler.get_pool(cr.dbname)
        
        cr.execute("Select project_id from kdvn_project_contract_rel \
                where contract_id in (%s)" %','.join(map(str,data['ids'])))
                                            
        prj_ids = []
        for prj_id in cr.fetchall():
            prj_ids.append(prj_id)
        prj_ids= ','.join(map(str,prj_ids))
        value = {
            'domain': "[('id', 'in',["+prj_ids+"])]",
            'name': 'Project',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'kdvn.project',
            'type': 'ir.actions.act_window',
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_project, 'state':'end'}
        }
    }
wiz_contract_to_project_open('kdvn.contract.project.open')
